home *** CD-ROM | disk | FTP | other *** search
- Path: howland.reston.ans.net!psinntp!psinntp!psinntp!psinntp!usenet
- From: grantp@usa.pipeline.com(Pete Grant)
- Newsgroups: comp.lang.c++,
- Subject: Re: Derivation and calling virtual functions
- Date: 28 Mar 1996 23:16:07 GMT
- Organization: Kalevi, Inc.
- Message-ID: <4jf6jn$gni@news1.h1.usa.pipeline.com>
- References: <graphix.828032689@spiff.cc.iastate.edu>
- NNTP-Posting-Host: 38.8.120.15
- X-PipeUser: grantp
- X-PipeHub: usa.pipeline.com
- X-PipeGCOS: (Pete Grant)
- X-Newsreader: Pipeline v3.5.0
-
- On Mar 28, 1996 17:04:49 in article <Derivation and calling virtual
- functions>, 'graphix@iastate.edu (Kent A Vander Velden)' wrote:
-
-
- >Say we have the following:
- >
- >class Base {
- >public:
- >virtual void func() { // some stuff }
- >virtual void write() { func(); };
- >};
- >
- >class Derived : public Base {
- >public:
- >void func() { // some stuff }
- >write(); { Base::func(); }
- >};
- >
- >Derived inst;
- >
- >Now, when I call inst.write() it in turn calls Bass::write() which in
- >turn calls Base::func(). Is there a way to instead have it call
- >Derived::func() if the instance is of type Derived? I hoped the
- >virtual keyword would help in this case.
-
- No, inst.write() calls Base::func(). If you recoded your sample
- so that Derived::write() called Base::write(), you'd find that
- Derived::func() would wind up being called. (Unless your
- compiler is broken).
-
- --
- Pete Grant
- Kalevi, Inc.
- Software Engineering & development
-